﻿#include "stdafx.h"

#define TEST_WKE_WND_CLASS   L"TestTransparent"
#define FGPID             ((WCHAR*)31117)



// 内置的 JsCall 调用
wkeJSValue  WKE_CALL onJsCall( wkeJSState* es );

// 窗口 PAINT 回调
bool WKE_CALL windowOnPaint( wkeWebView* webView, unsigned char* bits, unsigned w, unsigned h, HDC dst );


// 窗口定义
class CMainFrame{
public:
	CMainFrame( HINSTANCE hInstance, int nCmdShow ) : webView(0) {
		m_hWnd = CreateWindowExW( WS_EX_LAYERED, TEST_WKE_WND_CLASS, NULL, WS_POPUP, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, this );
		if( m_hWnd ){
			EweWindow* ewe = wkeGetFromWindow( wkeCreateWindow( m_hWnd ) );
			webView = wkeGetWebView(  ewe );
			wkeSetTransparent( webView, true );
			wkeSetUserData( webView, this );

			wkeLimitPlugins( webView, true );
			wkeWindowOnPaint( ewe, windowOnPaint );
			wkeOnJsCall( webView, onJsCall );

			MoveWindow( m_hWnd, 10, 10, 1000, 600, false );

			ShowWindow( (HWND)wkeGetHostWindow(webView), SW_NORMAL );
			ShowWindow( m_hWnd, SW_NORMAL);

			UpdateWindow( m_hWnd );

			wkeLoadFile( webView, L"1.html" ); // 打开页面

			UpdateControlPosition();
		}
	}

	LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
		switch (message){
		case WM_ERASEBKGND: return TRUE;
		case WM_SIZE: UpdateControlPosition(); break;
		case WM_DESTROY: PostQuitMessage(0); break;
		}
		return DefWindowProc(hWnd, message, wParam, lParam);
	}

	void UpdateControlPosition(){
		RECT rc;
		::GetClientRect( m_hWnd, &rc );
		if( webView ){
			::MoveWindow( (HWND)wkeGetHostWindow(webView), rc.left, rc.top, rc.right, rc.bottom, TRUE );
		}
	}

	wkeWebView* Webkit()  { return webView; }
	HWND        getHwnd() { return m_hWnd; }
private:
	HWND            m_hWnd;
	wkeWebView*     webView;
};

bool WKE_CALL windowOnPaint( wkeWebView* webView, unsigned char* bits, unsigned w, unsigned h, HDC dst ){
	CMainFrame* frame = (CMainFrame*)wkeGetUserData( webView );

	// 窗口透明处理与贴图
	PaintLayeredWindow( frame->getHwnd(), dst, (HDC)wkeGetViewDC(webView) );
	return true;
}

// 内置的 JsCall 调用
wkeJSValue  WKE_CALL onJsCall( wkeJSState* es ){
	int argc = wkeJSParamCount(es);
	if( argc > 0 ){
		wkeWebView* webView = wkeJSGetWebView( es );
		CMainFrame* frame = (CMainFrame*)wkeGetUserData( webView );

		const wchar_t* str = wkeJSToString( es, wkeJSParam(es,0) );
		if( wcsicmp( str, L"moveFrame" ) == 0 ){
			ReleaseCapture();
			PostMessage( frame->getHwnd(), WM_NCLBUTTONDOWN, HTCAPTION, 0 );
		}else if( wcsicmp( str, L"exit" ) == 0 ){
			PostQuitMessage(0);
		}
		else{
			for( int i=0; i<argc; i++){
				MessageBoxW( frame->getHwnd(), wkeJSToString( es, wkeJSParam(es,i) ), L"JsCall", MB_OK );
			}
		}
	}
	return wkeJSFalse(es);
}

// 窗口类定义
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
	if( message == WM_CREATE ){
		CREATESTRUCT *pcs=(CREATESTRUCT*)lParam;
		SetPropW( hWnd, FGPID, pcs->lpCreateParams );
	}
	CMainFrame* pthis = (CMainFrame*)GetPropW( hWnd, FGPID );
	return pthis ? pthis->WndProc( hWnd, message, wParam, lParam) : DefWindowProc(hWnd, message, wParam, lParam);
}

ATOM MyRegisterClass(HINSTANCE hInstance){
	WNDCLASSEX wcex={0};
	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszClassName	= TEST_WKE_WND_CLASS;
	return RegisterClassEx(&wcex);
}

// 程序入口
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow){
	wkeDisableWOFF( false );

	MyRegisterClass(hInstance);
	CMainFrame frame( hInstance, nCmdShow);

 	MSG msg;
	while( GetMessage( &msg, NULL, 0U, 0U ) ){
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return (int) msg.wParam;
}


